import simpleGit, { SimpleGit } from 'simple-git';

async function fetchFileHistory(repositoryPath: string, filePath: string) {
    const git: SimpleGit = simpleGit(repositoryPath);
    let hash_lst:string[] = new Array();
    try {
        // Pull latest changes (optional)
        await git.pull();

        // Fetch commit history of the specified file
        const logSummary = await git.log({ file: filePath });

        console.log(`History of file: ${filePath}`);
        logSummary.all.forEach((commit) => {
            console.log(`Commit: ${commit.hash} - ${commit.message}`);
            hash_lst.push(commit.hash);
            console.log(`Author: ${commit.author_name} <${commit.author_email}>`);
            console.log(`Date: ${commit.date}`);
            console.log('------------------------');
        });

        console.log(hash_lst);
        
    } catch (error) {
        console.error('Error fetching file history:', error);
    }
}

async function fetchFileContentAtCommit(repositoryPath: string, filePath: string, commitHash: string) {
    const git: SimpleGit = simpleGit(repositoryPath);

    try {
        // Fetch file content at the specified commit
        const fileContent = await git.show([`${commitHash}:${filePath}`]);

        console.log(`Content of ${filePath} at commit ${commitHash}:`);
        console.log(fileContent);
    } catch (error) {
        console.error('Error fetching file content:', error);
    }
}

// Example usage:
const repositoryPath = './'; // Replace with your Git repository path
const filePath = 'README.md'; // Replace with the path to the file in your repository

fetchFileHistory(repositoryPath, filePath);

const commitHash = '306d9ff4f24d2ca5b861a38c3972265ed83be005'; // Replace with the commit hash you want to fetch

fetchFileContentAtCommit(repositoryPath, filePath, commitHash);
